from PyQt6.QtCore import QFileInfo
from PyQt6.QtWidgets import QFrame, QPushButton, QFileDialog
from .ui.syncFileWidget import Ui_FileSyncWindow


class SyncFileInterface(QFrame, Ui_FileSyncWindow):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName("文件同步")
        self.setupUi(self)

        # 组件间距
        self.verticalLayout_2.setSpacing(20)

        self.syncProgress.setRange(0, 100)

        # 初始化信号槽
        self.__connect_signal_to_slot()

    def __sync_mode_clicked(self, button: QPushButton):
        """sync mode clicked slot"""
        print(f"文件同步模式更改为：{button.objectName()}")

        # 模式变化，清空文件路径
        self.orgFilePath.setText(None)
        self.copyFilePath.setText(None)

    def __org_file_clicked(self):
        """org file clicked slot"""
        button = self.syncModeGrop.checkedButton()

        file_dialog = QFileDialog()
        file_path = None
        if button.objectName() == 'syncFile':
            file_path, _ = file_dialog.getOpenFileName(self, "选择文件")
        else:
            file_path = file_dialog.getExistingDirectory(self, "选择文件夹")

        if file_path:
            file_info = QFileInfo(file_path)
            self.orgFilePath.setText(file_info.absoluteFilePath())

    def __copy_file_clicked(self):
        """copy file clicked slot"""
        button = self.syncModeGrop.checkedButton()

        file_dialog = QFileDialog()
        file_path = None
        if button.objectName() == 'syncFile':
            file_path, _ = file_dialog.getOpenFileName(self, "选择文件")
        else:
            file_path = file_dialog.getExistingDirectory(self, "选择文件夹")

        if file_path:
            file_info = QFileInfo(file_path)
            self.copyFilePath.setText(file_info.absoluteFilePath())

    def __start_sync(self):
        """start sync"""
        print("start sync")

        print("start sync 进度条: " + str(self.syncProgress.maximum()))
        print("start sync 进度: " + str(self.syncProgress.value()))
        self.syncProgress.setValue(self.syncProgress.value() + 10)
        self.fileSyncInfoList.insertItem(0, "序号：" + str(self.syncProgress.value()))

        # 关闭其他功能按键
        self.selectOrgFilePath.setEnabled(False)
        self.selectCopyFilePath.setEnabled(False)
        self.syncFile.setEnabled(False)
        self.syncDir.setEnabled(False)

        if self.syncProgress.value() + 10 > 100:
            self.startSync.setText("停止同步")

            self.syncProgress.setValue(100)

            self.selectOrgFilePath.setEnabled(True)
            self.selectCopyFilePath.setEnabled(True)
            self.syncFile.setEnabled(True)
            self.syncDir.setEnabled(True)

    def __connect_signal_to_slot(self):
        # 同步模式
        self.syncModeGrop.buttonClicked.connect(self.__sync_mode_clicked)

        # 源文件选择
        self.selectOrgFilePath.clicked.connect(self.__org_file_clicked)

        # 源文件选择
        self.selectCopyFilePath.clicked.connect(self.__copy_file_clicked)

        # 开始同步按钮
        self.startSync.clicked.connect(self.__start_sync)
